package com.lzdongrui.backend.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lzdongrui.backend.domain.vo.CampusVO;
import com.lzdongrui.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lzdongrui.common.annotation.Log;
import com.lzdongrui.common.core.controller.BaseController;
import com.lzdongrui.common.core.domain.AjaxResult;
import com.lzdongrui.common.enums.BusinessType;
import com.lzdongrui.backend.domain.Campus;
import com.lzdongrui.backend.service.ICampusService;
import com.lzdongrui.common.utils.poi.ExcelUtil;
import com.lzdongrui.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 校区Controller
 * 
 * @author wyh
 * @date 2023-04-28
 */
@RestController
@RequestMapping("/backend/campus")
public class CampusController extends BaseController
{
    @Autowired
    private ICampusService campusService;

    /**
     * 查询校区列表
     */
    @PreAuthorize("@ss.hasPermi('backend:campus:list')")
    @GetMapping("/list")
    public TableDataInfo list(Campus campus)
    {
        startPage();
        List<Campus> list = campusService.selectCampusList(campus);
        return getDataTable(list);
    }

    /**
     * 查询校区列表
     */
    @GetMapping("/all-list")
    public TableDataInfo allList(Campus campus)
    {
        List<Campus> list = campusService.selectCampusList(campus);
        return getDataTable(list);
    }


    /**
     * 导出校区列表
     */
    @PreAuthorize("@ss.hasPermi('backend:campus:export')")
    @Log(title = "校区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Campus campus)
    {
        List<Campus> list = campusService.selectCampusList(campus);
        ExcelUtil<Campus> util = new ExcelUtil<Campus>(Campus.class);
        util.exportExcel(response, list, "校区数据");
    }

    /**
     * 获取校区详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:campus:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(campusService.selectCampusById(id));
    }

    /**
     * 新增校区
     */
    @PreAuthorize("@ss.hasPermi('backend:campus:add')")
    @Log(title = "校区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Campus campus)
    {
        return toAjax(campusService.insertCampus(campus));
    }

    /**
     * 修改校区
     */
    @PreAuthorize("@ss.hasPermi('backend:campus:edit')")
    @Log(title = "校区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Campus campus)
    {
        return toAjax(campusService.updateCampus(campus));
    }

    /**
     * 删除校区
     */
    @PreAuthorize("@ss.hasPermi('backend:campus:remove')")
    @Log(title = "校区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(campusService.deleteCampusByIds(ids));
    }


    /**
     * 下载模板
     *
     * @param response
     */
    @Anonymous
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CampusVO> util = new ExcelUtil<CampusVO>(CampusVO.class);
        util.importTemplateExcel(response, "校区数据");
    }

    /**
     * 导入校区数据
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "校区", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('backend:campus:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        Long Operator = getUserId();
        ExcelUtil<Campus> util = new ExcelUtil<Campus>(Campus.class);
        List<Campus> campuses = util.importExcel(file.getInputStream());
        String message = campusService.importCampus(campuses, updateSupport,Operator);
        return success(message);
    }
}
